<?php
session_start();
include('databaseconnection.php');

$email=$_SESSION['email'];

$result=mysql_query("SELECT * FROM system_user WHERE email='$email'");
if(!$result) echo "invalid";

if(mysql_num_rows($result) == 1) 
{
$dd=gmdate(DATE_ATOM,time());
$d=explode("T",$dd);

$term=$_POST['term'];
 
  $domain=$_POST['domain'];
  $newdomain=$_POST['newdomain'];
  $language=$_POST['language'];
  $newlanguage=$_POST['newlanguage'];
  $accepted_meaning=$_POST['meaning'];
 // $suggested_by="vvjv";
  $suggested_date=$d[0];//"2012-02-21";
  
  $accepted_date=$d[0];//"2012-02-21";
  $votes=0;
  $comments="";

if($term=="" || $accepted_meaning=="")
{
header("Location:addterm.php?msg=Term and Accepted meaning are not null.......");
}
else if($domain=="" && $newdomain=="")
{
header("Location:addterm.php?msg=Domain are not null..... please select one of the domain or add the new domain of your term...");
}
else if($domain!="" && $newdomain!="")
{
header("Location:addterm.php?msg=Please select one of the domain Or add the new domain of your term...");
}
else if($language=="" && $newlanguage=="")
{
header("Location:addterm.php?msg=Language are not null..... please select one of the language or add the language...");
}
else if($language!="" && $newlanguage!="")
{
header("Location:addterm.php?msg=please select one of the language Or add the language...");
}
else
{
$resulte=mysql_query("SELECT * FROM terms WHERE term='$term' and accepted_meaning='$accepted_meaning'");
if(!$resulte) echo "invalid";

if(mysql_num_rows($resulte) == 1) 
{
header("Location:addterm.php?msg=Term and Accepted meaning are already exist.....");
}
else
{
$result=mysql_query("SELECT * FROM user WHERE email='$email'");
if(!$result) echo "invalid";
while($row=mysql_fetch_array($result))
{

$username=$row['first_name'];


  if($language=="")
  {
  $resultsl=mysql_query("SELECT * FROM language WHERE language='$newlanguage' ");
  if (mysql_num_rows($resultsl) == 1) 
		{
		  $ilanguage=$newlanguage;
		}
		else
		{
  $ilanguage=$newlanguage;
  $query="INSERT INTO language (language) values ('$newlanguage')";
  $rez1=mysql_query($query);
  if(!$rez1) {echo "INvalid".mysql_error();}
  }
  }
  if($newlanguage=="")
  {
  $ilanguage=$language;
  }








  if($domain=="")
  {
  $resultsd=mysql_query("SELECT * FROM domain WHERE domain='$newdomain' ");
  if (mysql_num_rows($resultsd) == 1) 
		{
		  $idomain=$newdomain;
		}
		else
		{
  $idomain=$newdomain;
  $query="INSERT INTO domain (domain) values ('$newdomain')";
  $rez1=mysql_query($query);
  if(!$rez1) {echo "INvalid".mysql_error();}
  }
  }
  if($newdomain=="")
  {
  $idomain=$domain;
  }
  
  $query="INSERT INTO terms (term, domain, accepted_meaning, language, suggested_by, suggested_date, checked_by, accepted_date,votes,comments) VALUES ('$term', '$idomain', '$accepted_meaning', '$ilanguage', '$username', '$suggested_date', '$username', '$accepted_date','$votes','$comments')";
  $rez=mysql_query($query);
  if(!$rez) {echo "INvalid".mysql_error();}
  else{
  header("Location:addterm.php?msg=term is added........");
  }
  }
  }
  }
  }
  else
  {
	header("Location:addterm.php?msg=administrator only allow to add term....you can suggest term");
  }
  ?>